package net.kazed.nextaction.task;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import net.kazed.nextaction.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.res.Resources;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class DateInput {
    
    private Activity parent;
    private DateFormat dateFormat;
    private String noDateLabel;
    private TextView dateDisplayField;
    private Button setDateButton;
    private Button clearDateButton;
    private Date date;

    public DateInput(final Activity parent, int textFieldId, int setButtonId, int clearButtonId, final int dialogId) {
        
        this.parent = parent;
        
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Resources resources = parent.getResources();
        noDateLabel = resources.getString(R.string.no_date_set);
        
        dateDisplayField = (TextView) parent.findViewById(textFieldId);
        setDateButton = (Button) parent.findViewById(setButtonId);
        setDateButton.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               parent.showDialog(dialogId);
           }
        });
        
        clearDateButton = (Button) parent.findViewById(clearButtonId);
        clearDateButton.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               setDate(null);
           }
        });
    }

    /**
     * @return Selected date;
     */
    public Date getDate() {
        return date;
    }

    /**
     * @param date Selected date.
     */
    public void setDate(Date date) {
        this.date = date;
        if (date == null) {
            dateDisplayField.setTextKeepState(noDateLabel);
        } else {
            dateDisplayField.setTextKeepState(dateFormat.format(date));
        }
    }

    /**
     * @return Created dialog.
     */
    public Dialog createDialog() {
        Dialog dialog = null;
        DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar calendar = GregorianCalendar.getInstance();
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, monthOfYear);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                calendar.set(Calendar.HOUR, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                date = calendar.getTime();
                dateDisplayField.setTextKeepState(dateFormat.format(date));
            }
        };
        int year = 2009;
        int month = 1;
        int day = 1;
        dialog = new DatePickerDialog(parent, dateSetListener, year, month, day);
        return dialog;
    }
    
    /**
     * Update date in dialog.
     * @param dialog Is a DatePickerDialog.
     */
    public void updateDialog(Dialog dialog) {
        DatePickerDialog dateDialog = (DatePickerDialog) dialog;
        Calendar calendar = GregorianCalendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        dateDialog.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH));

    }
}
